package miniBanco.Handler;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import miniBanco.Core.*;
import miniBanco.Dto.*;
import miniBanco.Gui.*;
import miniBanco.Mapper.MapperRegistry;
import miniBanco.Mapper.UsuarioDAO;
import miniBanco.Mapper.Interface.IClienteFinder;

public class HandlerMain {
	static HandlerMain instance;
	WinLoginCliente loginCliente;
	WinLoginUsuario loginUsuario;
	IClienteFinder clienteFinder;
	MenuFrame frame;
	UsuarioBO userBO;
	private HandlerMain()
	{
		clienteFinder = MapperRegistry.GetInstance().ClienteMapper();
		userBO = new UsuarioBO();
		userBO.setDao(new UsuarioDAO());
	}	
	public static HandlerMain getInstance()
	{
		if(instance == null)
			instance = new HandlerMain();
		return instance;
	}
	public void Inicializar()
	{
		final WinSeleccionoIngreso win = new  WinSeleccionoIngreso();
		win.getBtnIngresoCliente().addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				loginCliente();
				win.setVisible(false);
			}
		});
		win.getBtnIngresoUsuario().addActionListener(new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				login();
				win.setVisible(false);
			}
		});
		win.setVisible(true);
	}
	private void login()
	{
		loginUsuario = new WinLoginUsuario();
		loginUsuario.getButtonAceptar().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				UsuarioDTO userDTO = loginUsuario.getFormValues();
				if(userDTO.getUsername().equals("") || userDTO.getPassword().equals(""))
				{
		    		JOptionPane.showMessageDialog(null, "Usuario no valido");
					return;
				}
				if(userDTO.getUsername().toLowerCase().equals("admin") && userDTO.getPassword().toLowerCase().equals("admin"))
				{
					LoginExitoso();
				}		
				if(userBO.IsUsuarioSistema(userDTO.getUsername(), userDTO.getPassword()))
				{
					LoginExitoso();
				}
			}
		});
		loginUsuario.setVisible(true);
		return;
	}
	private void ArmoMainFrame()
	{
		frame = new MenuFrame();	
		JLabel labelEast = new JLabel("East");	
		JLabel labelSouth = new JLabel("South");			
		JLabel labelWest = new JLabel("West");	
		JLabel labelNorth = new JLabel("North");				
//		
		labelEast.setBackground(Color.magenta);
		labelWest.setBackground(Color.ORANGE);		
		labelSouth.setBackground(Color.PINK);	
		labelNorth.setBackground(Color.lightGray);			
//
		labelEast.setOpaque(true);
		labelWest.setOpaque(true);
		labelSouth.setOpaque(true);		
		labelNorth.setOpaque(true);		
//		
		frame.add(labelWest,BorderLayout.WEST);
		frame.add(labelSouth,BorderLayout.SOUTH);
	}
	private void LoginExitoso()
	{
		loginUsuario.setVisible(false);
		ArmoMainFrame();
		HandlerUsuariosMainFrame usuarioMainFrame = new HandlerUsuariosMainFrame(this,null);
		LogGridPanel logGrid = HandlerLog.getInstance().VerLog();
		frame.add(logGrid,BorderLayout.EAST);
		frame.SetUpForUsuario();
		usuarioMainFrame.Show();
		frame.setVisible(true);
	}
	private void LoginClienteExitoso(Cliente c)
	{
		loginCliente.setVisible(false);
		ArmoMainFrame();
		HandlerClienteMainFrame clienteMainFrame = new HandlerClienteMainFrame(this,c);
		LogGridPanel logGrid = HandlerLog.getInstance().VerLog();

		frame.add(logGrid,BorderLayout.EAST);
		clienteMainFrame.Show();
		frame.SetUpForUsuario();
		frame.setVisible(true);
	}

	private void loginCliente()
	{
		loginCliente = new WinLoginCliente();	
		loginCliente.getButtonAceptar().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ClienteLoginDTO cliDTO = loginCliente.getFormValues();
				if(cliDTO.getCliente().equals(""))
				{
		    		JOptionPane.showMessageDialog(null, "Cliente no valido");
					return;
				}
				ArrayList<Cliente> clienteList = clienteFinder.findByNombre(cliDTO.getCliente());
				 
				if(clienteList.size() > 0)
				{
					LoginClienteExitoso(clienteList.get(0));
				}
			}
		});
		loginCliente.setVisible(true);
		return;
	}

}
